package org.liuqm.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {

    public static <T>T getMapper(Class clazz) throws IOException {
        String mybatisConfig = "mybatis-config.xml";

        InputStream inputStream = Resources.getResourceAsStream(mybatisConfig);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        SqlSession session = sqlSessionFactory.openSession(true);
        //获取mapper接口的代理实现类
        T t = (T) session.getMapper(clazz);
        return t;
    }
}
